package com.roadjava.tutor.handler.back;

import com.roadjava.tutor.bean.dto.OrderInfoDTO;
import com.roadjava.tutor.bean.dto.TeacherJobDTO;
import com.roadjava.tutor.bean.dto.UserDTO;
import com.roadjava.tutor.bean.enums.OrderStatusEnum;
import com.roadjava.tutor.bean.enums.RoleEnum;
import com.roadjava.tutor.bean.res.ResultDTO;
import com.roadjava.tutor.bean.res.TableResult;
import com.roadjava.tutor.bean.vo.OrderInfoVO;
import com.roadjava.tutor.bean.vo.TeacherJobVO;
import com.roadjava.tutor.bean.vo.UserVO;
import com.roadjava.tutor.service.AdminService;
import com.roadjava.tutor.service.MemberService;
import com.roadjava.tutor.service.OrderInfoService;
import com.roadjava.tutor.util.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 会员/管理员 统一入口控制器
 * @author zhaodaowen
 * @see <a href="http://www.roadjava.com">乐之者java</a>
 */
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoHandler {
    @Resource
    private OrderInfoService orderInfoService;

    @PostMapping("/add") @ResponseBody
    public ResultDTO<String> add(OrderInfoDTO dto,HttpSession session){
        UserVO attribute = (UserVO) session.getAttribute(Constants.USER_SESSION_KEY);
        if (RoleEnum.ADMIN.getCnName().equals(attribute.getRole())) {
            return ResultDTO.buildFailure("管理员不能参与预约");
        }
        dto.setStatus(OrderStatusEnum.WAIT_FOR_AGREE.getStatus());
        return orderInfoService.add(dto);
    }
    /**
     * 订单列表查询
     */
    @PostMapping("/loadTable") @ResponseBody
    public ResultDTO<TableResult<OrderInfoVO>> loadTable(OrderInfoDTO dto){
        TableResult<OrderInfoVO> tableResult=new TableResult<>();
        ResultDTO<List<OrderInfoVO>> result = orderInfoService.loadTable(dto);
        tableResult.setTotalCount(result.getTotal());
        tableResult.setRows(result.getData());
        return ResultDTO.buildSuccess(tableResult);
    }

    @PostMapping("/update") @ResponseBody
    public ResultDTO<String> update(OrderInfoDTO dto){
        return orderInfoService.update(dto);
    }
}
